package com.woniuxy.woniuticket.usermodule.service.Imp;


import com.woniuxy.woniuticket.movieinfomodule.dao.UsersInfoDAO;
import com.woniuxy.woniuticket.pojo.UsersInfo;
import com.woniuxy.woniuticket.pojo.UsersLogin;
import com.woniuxy.woniuticket.usermodule.dao.UsersLoginDAO;
import com.woniuxy.woniuticket.usermodule.service.UserLoginService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class UserLoginServiceImp implements UserLoginService {
    @Resource
    private UsersLoginDAO usersLoginDAO;
    @Resource
    private UsersInfoDAO usersInfoDAO;
    //用户登录
    //@Transactional(rollbackFor = Exception.class)
    @Override
    public String login(UsersLogin usersLogin){
        UsersLogin user = usersLoginDAO.selectUserByAccount(usersLogin);
        if(user != null){
            if(usersLogin.getPassword().equals(user.getPassword())){
                return "登录成功";
            }else{
                return "用户名或密码错误";
            }
        }else{
            return "用户名不存在";
        }
    }
    //注册

    @Override
    public String regist(UsersLogin usersLogin) {
        UsersLogin user  = usersLoginDAO.selectUserByAccount(usersLogin);
        if (user == null) {
            //插入一条数据到info中并获取插入之后的ID
            UsersInfo info = new UsersInfo();
            info.setTelnumber(usersLogin.getAccount());
            usersInfoDAO.insert(info);
            //获取info的ID
            UsersInfo info1 = usersInfoDAO.queryIdByTel(usersLogin.getAccount());

            usersLogin.setUserInfoId(info1.getId());
            usersLoginDAO.insert(usersLogin);
            return "注册成功";
        }else{
            return "用户名已存在";
        }

    }
    //修改密码
    public String updatePas(UsersLogin usersLogin) {
        UsersLogin user  = usersLoginDAO.selectUserByAccount(usersLogin);
        if (user == null) {
            return "用户名不存在";
        }else{
            usersLoginDAO.updatePas(usersLogin);
            return "修改成功";
        }
    }
    //通过账号 获取登录表信息
    public UsersLogin selectUserByAccount(UsersLogin usersLogin){
        return usersLoginDAO.selectUserByAccount(usersLogin);
    }
}
